package com.ifast.asset.controller;


import java.util.Arrays;

import com.ifast.asset.domain.IpsegmentDO;
import com.ifast.asset.service.IpsegmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-03-15 10:45:44 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/ipsegment")
public class IpsegmentController extends BaseController {
	@Autowired
	private IpsegmentService ipsegmentService;
	
	@GetMapping()
	//@RequiresPermissions("wxmp:ipsegment:ipsegment")
	String Ipsegment(){
	    return "wxmp/ipsegment/ipsegment";
	}
	
	@ResponseBody
	@GetMapping("/list")
	//@RequiresPermissions("wxmp:ipsegment:ipsegment")
	Result<Page<IpsegmentDO>> list(IpsegmentDO ipsegmentDTO){
        Wrapper<IpsegmentDO> wrapper = new EntityWrapper<>(ipsegmentDTO).orderBy("id", false);
        Page<IpsegmentDO> page = ipsegmentService.selectPage(getPage(IpsegmentDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	//@RequiresPermissions("wxmp:ipsegment:add")
	String add(){
	    return "wxmp/ipsegment/add";
	}

	@GetMapping("/edit/{id}")
	//@RequiresPermissions("wxmp:ipsegment:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		IpsegmentDO ipsegment = ipsegmentService.selectById(id);
		model.addAttribute("ipsegment", ipsegment);
	    return "wxmp/ipsegment/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("wxmp:ipsegment:add")
	public Result<String> save( IpsegmentDO ipsegment){
		ipsegmentService.insert(ipsegment);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("wxmp:ipsegment:edit")
	public Result<String>  update( IpsegmentDO ipsegment){
		boolean update = ipsegmentService.updateById(ipsegment);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("wxmp:ipsegment:remove")
	public Result<String>  remove( Integer id){
		ipsegmentService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("wxmp:ipsegment:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		ipsegmentService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	//检测  新提添加的ip是否在  已有的网段中  如果没有  则添加
	@GetMapping("/checkIpSegment")
	@ResponseBody
	public Result<String>  checkIpSegment(String ip){
		ipsegmentService.checkIpSegment(ip);
		return Result.ok();
	}
}
